PGDB="postgresql://bilin_prom_rw:S6Z*zZL&Fd%QyJW6@10.1.3.5:5432/bilin_prom?sslmode=disable"
MIG_DIR=db/migration
ARCHERY_DIR=db/archery
RPC_DIR=./rpc
TARGET_DIR=dist
APP=bilin-prom

# Go parameters
GOCMD=GO111MODULE=on go
GOBUILD=$(GOCMD) build
GODOWNLOAD=$(GOCMD) mod download

v:
	migrate -path db/migration -database ${PGDB} version
up:
	migrate -path db/migration -database ${PGDB} -verbose up

down:
	migrate -path db/migration -database ${PGDB} -verbose down

goto:
	migrate -path db/migration -database ${PGDB} -verbose goto 2

up1:
	migrate -path db/migration -database ${PGDB} -verbose up 1

down1:
	migrate -path db/migration -database ${PGDB} -verbose down 1

dbnext:
ifdef seq
	migrate create -ext sql -dir ${MIG_DIR} -seq $(seq)
else
	migrate create -ext sql -dir ${MIG_DIR} -seq bilin_prom
endif

archery:
ifdef seq
	echo '/* generated by makefile for $(seq) */\n' > $(ARCHERY_DIR)/$(seq)-schema.sql
	echo '/* $(seq).up.sql */' >> $(ARCHERY_DIR)/$(seq)-schema.sql
	cat $(MIG_DIR)/$(seq).up.sql >> $(ARCHERY_DIR)/$(seq)-schema.sql
ifneq (,$(wildcard $(MIG_DIR)/$(seq).constraint.sql))
	echo '/* $(seq).constraint.sql */' >> $(ARCHERY_DIR)/$(seq)-schema.sql
	cat $(MIG_DIR)/$(seq).constraint.sql >> $(ARCHERY_DIR)/$(seq)-schema.sql
endif
ifneq (,$(wildcard $(MIG_DIR)/$(seq).owner.sql))
	echo '/* $(seq).owner.sql */' >> $(ARCHERY_DIR)/$(seq)-schema.sql
	cat $(MIG_DIR)/$(seq).owner.sql >> $(ARCHERY_DIR)/$(seq)-schema.sql
endif
else
	echo '/* generated by makefile for all */\n' > $(ARCHERY_DIR)/schema.sql
	echo '/* all.up.sql */' >> $(ARCHERY_DIR)/schema.sql
	cat $(MIG_DIR)/*.up.sql >> $(ARCHERY_DIR)/schema.sql
	echo '/* all.constraint.sql */' >> $(ARCHERY_DIR)/schema.sql
	cat $(MIG_DIR)/*.constraint.sql >> $(ARCHERY_DIR)/schema.sql
	# echo '/* all.owner.sql */' >> $(ARCHERY_DIR)/schema.sql
	# cat $(MIG_DIR)/*.owner.sql >> $(ARCHERY_DIR)/schema.sql
endif

sqlc:
	sqlc generate

pb: 
	$(RPC_DIR)/gen.sh

test:
	go test -v -cover ./...

dev:
	go run cmd/main.go

priv:
	go env -w GOPRIVATE=code.labs.luedongtech.com
	go get code.labs.luedongtech.com/backend/ldkit/cmd/protoc-gen-swagger@latest

mock:
	mockgen -package mockdb -destination db/mock/store.go github.com/techschool/simplebank/db/sqlc Store

download:
	$(GODOWNLOAD)

build:
	rm -rf $(TARGET_DIR)
	mkdir -p $(TARGET_DIR)
	#cp conf/config.toml $(TARGET_DIR)/config.toml
	$(GOBUILD) -o $(TARGET_DIR)/$(APP) cmd/main.go

clean:
	rm -rf $(TARGET_DIR)

run:
    # 启动 bilin-prom 应用
	nohup $(TARGET_DIR)/$(APP) -c=$(TARGET_DIR)/config.toml 2>&1 > $(TARGET_DIR)/$(APP).log &

stop:
    # 关闭 bgs 应用
	pkill -f $(TARGET_DIR)/$(APP)

#.PHONY: migrateup migratedown up2 down2 sqlc test server mock
